package com.example.base;

public class MyArray {

    private int[] array;

    private int size;

    public MyArray(int capacity) {
        this.array = new int[capacity];
        size = 0;
    }

    /**
     * 数组插入元素
     * @param element
     * @param index
     * @throws Exception
     */
    public void insert(int element,int index) throws Exception{
        //判断访问下标是否超出范围
        if(index<0 || index>size) {
            throw new IndexOutOfBoundsException("超出数组实际元素范围");
        }
        //从右向左循环，将元素逐个向右挪1位
        for(int i=size-1; i>=index; i--){
            array[i+1]= array[i];
        }
        //腾出的位置放入新元素
        array[index] = element;
        size++;
    }


    /**
     * 输出数组
     */
    public void output(){
        for(int i=0;i<size;i++){
            System.out.println(array[i]);
        }
    }

    /**
     * 数组删除元素
     * @param index
     * @return
     * @throws Exception
     */
    public int delete(int index) throws Exception {
        //判断访问下标是否超出范围
        if(index<0 || index>=size){
            throw new IndexOutOfBoundsException("超出数组实际元素范围！");
        }
        int deletedElement = array[index];
        //从左向右循环，将元素逐个向左挪1位
        for(int i=index; i<size-1; i++){
            array[i] = array[i+1];
        }
        size--;
        return deletedElement;
    }


    public static void main(String[]args)throws Exception {
        MyArray myArray = new MyArray(10);
        myArray.insert(3, 0);
        myArray.insert(7, 1);
        myArray.insert(9, 2);
        myArray.insert(5, 3);
        myArray.output();
        System.out.println("--------------------------------------------------------------------");
        myArray.delete(1);
        myArray.output();
    }

}